<?php
	/**
	 * Created by lzx.
	 * User: lzx
	 * Date: 2016/7/18
	 * Time: 14:20
	 */
	
	namespace Admin\Controller;
	
	use Think\Controller;

	class ArticleController extends CommonController{
		public function index()
		{
			$p     = intval(I('get.p')) > 1 ? intval(I('get.p')) : 1;
			$where = array(
				'agent_id' => session('agent_id')
			);
			if ( I('get.search') ) {
				$where['title'] = array( 'like', '%' . I('get.search') . '%' );
			}
			if ( I('get.type') ) {
				$where['type_id'] = I('get.type');
			}
			if ( I('get.start') && I('get.end') ) {
				$where['create_time'] = array( 'between', array( strtotime(I('get.start')), strtotime(I('get.end') . ' +1 day') - 1 ) );
			}
			$cate = $this->getCate();
			$data = D('Article')->_list($where, $p);
			$page = page('article', $where);
			$this->assign('cate', $cate);
			$this->assign('page', $page);
			$this->assign('data', $data);
			$this->display();
		}

		public function articleAdd()
		{
			$cate = $this->getCate();
			$this->assign('cate', $cate);
			$this->display();
		}
		
		public function articleAddAction()
		{
			$data    = array(
				'title'       => I('post.title'),
				'type_id'     => I('post.type_id'),
				'keywords'    => I('post.keywords'),
				'sort'        => I('post.sort'),
				'abstract'    => I('post.abstract'),
				'author'      => I('post.author'),
				'thumb_image' => I('post.thumb'),
				'content'     => I('post.content')
			);
			$article = D('Article');
			if ( !$article->create($data) ) {
				$this->error($article->getError());
			}
			$res = $article->add();
			if ( !$res ) {
				$this->error('添加失败');
			}
			$this->success('添加成功', U('index'));
		}

		/**
		 * 删除文章
		 * @author lzx
		 */
		public function articleDel()
		{
			$id = I('get.article_id');
			if ( !is_numeric($id) && !is_array($id) ) {
				$this->error('文档错误');
			}
			$where = array(
				'agent_id' => session('agent_id'),
			);
			if ( is_array($id) ) {
				$where['article_id'] = array( 'in', $id );
			}
			if ( is_numeric($id) ) {
				$where['article_id'] = $id;
			}
			$res = M('article')->where($where)->delete();
			if ( !$res ) {
				$this->error('删除失败');
			}
			$this->success('删除成功');
		}

		/**
		 * 修改状态
		 * @author lzx
		 */
		public function changeStatus()
		{
			$id     = I('get.article_id');
			$status = I('get.status');
			if ( !is_numeric($id) ) {
				$this->error('文档错误');
			}
			if ( $status != 1 && $status != 2 ) {
				$this->error('状态错误');
			}
			$where = array(
				'article_id' => $id,
				'agent_id'   => session('agent_id')
			);
			$res   = M('article')->where($where)->setField('status', $status);
			if ( !$res ) {
				$this->error('修改失败');
			}
			$this->success('修改成功');
			
		}
		

		/**
		 * 编辑图文
		 * @author lzx
		 */
		public function articleEdit()
		{
			$id = I('get.article_id');
			if ( !is_numeric($id) ) {
				$this->error('文档错误');
			}
			$data = D('Article')->detail($id);
			$cate = $this->getCate();
			$this->assign('cate', $cate);
			$this->assign('data', $data);
			$this->display();
		}

		/**
		 * 编辑文章处理
		 * @author lzx
		 */
		public function articleEditAction()
		{
			$id = I('post.article_id');
			if ( !is_numeric($id) ) {
				$this->error('文档错误');
			}
			$data    = array(
				'article_id'  => $id,
				'title'       => I('post.title'),
				'abstract'    => I('post.abstract'),
				'keywords'    => I('post.keywords'),
				'type_id'     => I('post.type_id'),
				'author'      => I('post.author'),
				'sort'        => I('post.sort'),
				'thumb_image' => I('post.thumb'),
				'content'     => I('post.content'),
				'status'      => I('post.status') == 1 ? 1 : 2
			);
			$article = D('Article');
			$data    = $article->create($data);
			if ( !$data ) {
				$this->error($article->getError());
			}
			$where = array(
				'article_id' => $id,
				'agent_id'   => session('agent_id')
			);
			$res   = $article->where($where)->save();
			if ( !$res ) {
				$this->error('修改失败');
			}
			$this->success('修改成功');
		}

		/**
		 * ueditor 修改图片保存位置
		 * @author lzx
		 */
		public function ueditor()
		{
			$data = new \Org\Util\Ueditor();
			echo $data->output();
		}

		/**
		 * 上传缩略图
		 * @author lzx
		 */
		public function uploadThumb()
		{
			$data = upload_img('Uploads/');
			if ( !is_array($data) ) {
				$data['status'] = 0;
				exit( json_encode($data) );
			}
			$data['info']   = $data;
			$data['status'] = 1;
			exit( json_encode($data) );
		}

		/**
		 * 获取分类
		 * @return mixed
		 * @author lzx
		 */
		private function getCate()
		{
//			$where = array('agent_id'=>session('agent_id'));
			$data = M('cate')->getField('id,name', true);
			return $data;
		}
	}